package com.icinfo.cloud.provider.common.crontask;

import com.icinfo.cloud.provider.punish.common.datareport.service.impl.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 描述：数据上报定时任务
 *
 * @author xiangyuyu
 * @date 2022/4/13 19:07
 */
@Configuration
@EnableScheduling
@Slf4j
@RestController
@RequestMapping("/dataReportTask")
public class DataReportTask {
    private static final String PUNISH_DATA_REPORT_REDIS_KEY = "PUNISH:TASK:DATA_REPORT_REDIS_KEY";
    @Resource
    private CaseDataReportBasicServiceImpl basicService;
    @Resource
    private CaseDataReportInvestigateServiceImpl investigateService;
    @Resource
    private CaseDataReportObtainServiceImpl obtainService;
    @Resource
    private CaseDataReportInformServiceImpl informService;
    @Resource
    private CaseDataReportHearingServiceImpl hearingService;
    @Resource
    private CaseDataReportDecisionServiceImpl decisionService;
    @Resource
    private CaseDataReportExecuteServiceImpl executeService;
    @Resource
    private CaseDataReportCloseServiceImpl closeService;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 【V1.1.0】描述：数据上报
     *
     * @author xiangyuyu
     * @date 2022-04-13 19:14
     */
    @ApiOperation(value = "数据上报", httpMethod = "POST")
    @PostMapping("/report")
    //todo 重庆分支暂不数据上报，正式启动的时候再取消注释
    // @Scheduled(cron = "0 0 3 * * ?")
    public void startReport() {
        //防止多台服务器重复执行（过期时间设置为10分钟，预防服务器时间不同步）
        if (!redisTemplate.opsForValue().setIfAbsent(PUNISH_DATA_REPORT_REDIS_KEY, PUNISH_DATA_REPORT_REDIS_KEY, 10, TimeUnit.MINUTES)) {
            log.info("--------当前服务器未拿到分布式锁，不执行逻辑----------");
            return;
        }
        LocalDateTime startTime = LocalDateTime.now();
        log.info("startReport start-startTime:{}", startTime);
        //数据推送范围：今日（19号）3点开始推送2天前数据(17号3点前)
        List<String> baseInfoReportFailCaseIdList = new ArrayList<>();

        //基础信息
        List<String> reportFailCaseIdList = basicService.batchCaseReport(baseInfoReportFailCaseIdList);
        if(ObjectUtils.isEmpty(reportFailCaseIdList) && reportFailCaseIdList.size() > 0){
            baseInfoReportFailCaseIdList.addAll(reportFailCaseIdList);
        }

        //调查取证信息
        reportFailCaseIdList = investigateService.batchCaseReport(baseInfoReportFailCaseIdList);
        if(ObjectUtils.isEmpty(reportFailCaseIdList) && reportFailCaseIdList.size() > 0){
            baseInfoReportFailCaseIdList.addAll(reportFailCaseIdList);
        }

        //处罚告知信息
        reportFailCaseIdList = informService.batchCaseReport(baseInfoReportFailCaseIdList);
        if(ObjectUtils.isEmpty(reportFailCaseIdList) && reportFailCaseIdList.size() > 0){
            baseInfoReportFailCaseIdList.addAll(reportFailCaseIdList);
        }

        //处罚告知信息(听证类)
        reportFailCaseIdList = hearingService.batchCaseReport(baseInfoReportFailCaseIdList);
        if(ObjectUtils.isEmpty(reportFailCaseIdList) && reportFailCaseIdList.size() > 0){
            baseInfoReportFailCaseIdList.addAll(reportFailCaseIdList);
        }

        //处罚决定信息
        reportFailCaseIdList = decisionService.batchCaseReport(baseInfoReportFailCaseIdList);
        if(ObjectUtils.isEmpty(reportFailCaseIdList) && reportFailCaseIdList.size() > 0){
            baseInfoReportFailCaseIdList.addAll(reportFailCaseIdList);
        }

        //处罚执行信息
        reportFailCaseIdList = executeService.batchCaseReport(baseInfoReportFailCaseIdList);
        if(ObjectUtils.isEmpty(reportFailCaseIdList) && reportFailCaseIdList.size() > 0){
            baseInfoReportFailCaseIdList.addAll(reportFailCaseIdList);
        }

        //结案信息
        reportFailCaseIdList = closeService.batchCaseReport(baseInfoReportFailCaseIdList);
        if(ObjectUtils.isEmpty(reportFailCaseIdList) && reportFailCaseIdList.size() > 0){
            baseInfoReportFailCaseIdList.addAll(reportFailCaseIdList);
        }
        LocalDateTime endTime = LocalDateTime.now();
        Duration duration = Duration.between(startTime, endTime);
        long seconds = duration.getSeconds();
        log.info("startReport end-endTime:{},consume:{} s", endTime, seconds);
    }
}
